package Day23;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;

public class DoExpress{
    private Random r = new Random();
    private ArrayList<Express> list = new ArrayList<>();
    Socket socket = new Socket("localhost",65535);

    public DoExpress() throws IOException {
    }

    //添加快递
    public boolean add(Express e) throws IOException {
        Express a = findNumber(e.getNumber());
        if(a == null) {
            e.setCode(code());
            e.setGui(gui());
            list.add(e);
            OutputStream os = socket.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(list.toString());
            return true;
        }
        return false;
    }

    //随机生成取件码
    public int code() {
        if(list.size() == 100){
            return 0;
        }
        int a = r.nextInt(90000) + 10000;
        for (int i = 0; i < list.size(); i++) {
            if(a == list.get(i).getCode()){
                code();
            }
        }
        return a;
    }

    //随机生成快递柜
    public int gui(){
        if(list.size() == 100){
            return 0;
        }
        int a = r.nextInt(90) + 10;
        for (int i = 0; i < list.size(); i++) {
            if(a == list.get(i).getGui()){
                code();
            }
        }
        return a;
    }

    //查找快递
    public Express findNumber(String s){
        for (int i = 0; i < list.size(); i++) {
            if(s.equals(list.get(i).getNumber())){
                return list.get(i);
            }
        }
        return  null;
    }

    //修改快递
    public boolean revise(Express e) throws IOException {
        View v = new View();
        add(e);
        boolean a = delete(v.findNumber());
        return a;
    }

    //删除快递
    public boolean delete(String s) throws IOException {
        Express e = findNumber(s);
        boolean a = list.remove(e);
        OutputStream os = socket.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(e);
        return a;
    }

    //遍历所有快递
    public void printfAll() throws IOException, ClassNotFoundException {
        InputStream is = socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        list = (ArrayList<Express>) ois.readObject();
        if(list.size() == 0){
            return;
        }
        for (Express e:list) {
            System.out.println(e);
        }
    }

    //取快递
    public boolean user(int a) throws IOException, ClassNotFoundException {
        InputStream is = socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        list = (ArrayList<Express>) ois.readObject();
        for (int i = 0; i < list.size(); i++) {
            if(a == list.get(i).getCode()){
                list.remove(list.get(i));
                OutputStream os = socket.getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(os);
                oos.writeObject(list.toString());
                return true;
            }
        }
        return false;
    }
}
